package dataDictionary

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	. "go_tempate/SQL/table"
	. "go_tempate/common/function"
	. "go_tempate/common/store"
)

// 查询全部密保
// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
func AllSecurity(ctx *gin.Context) {
	body := ObtainResponseBody(ctx) // 获取当前的返回结构体

	//初始化 密保问题查询表
	var security = SecurityType{}

	//执行 密保问题查询表 中查询全部密保信息方法内容
	// ----------------------------------------------------------------------------------------------------------
	allSecurity, err := security.AllSecurity()

	//如果查询失败
	if err != nil {
		body.BatchSettings(ConfidentialityErr, SecurityInquiryError, nil)
		return
	}

	//声明返回前端的切片内容，长度与查询出来的总密保内容长度相等
	// ----------------------------------------------------------------------------------------------------------
	var bodyData = make([]SecurityItem, len(allSecurity))

	//进行循环给 bodyData 赋值
	for i, v := range allSecurity {
		bodyData[i].SecurityName = v.Name
		bodyData[i].SecurityId = v.ID
	}

	//返回数据完成后的数据内容
	body.Data = bodyData

	zap.S().Info("全部密保问题查询成功")
}

// 新增密保内容
// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
func AddSecurity(ctx *gin.Context) {
	body := ObtainResponseBody(ctx) // 获取当前的返回结构体

	//获取新增的密保问题
	// ----------------------------------------------------------------------------------------------------------
	content := ctx.Query("title")

	//如果没有传参
	if content == "" {
		body.BatchSettings(ConfidentialityErr, NoSecurityContentModified, nil)
		return
	}

	//初始化 密保问题查询表
	// ----------------------------------------------------------------------------------------------------------
	var security = SecurityType{Name: content}

	//创建此密保问题并入库
	err := security.CreateSecurity()

	//创建失败的话
	if err != nil {
		body.BatchSettings(ConfidentialityErr, SecurityInSqlError, nil)
		return
	}

	zap.S().Infof("密保问题:%v;新增成功", content)

}
